package com.inet.test03;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author weijingchen
 * @Date 2025/6/26 15:15
 * @Version 1.0.0
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10301);
        Socket socket = ss.accept();
        System.out.println("连接成功");


        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src/main/resources/f.txt"));
        int len;
        byte[] bytes = new byte[1024];
        while ((len = bis.read(bytes)) != -1) {
            System.out.println("len = " + len);
            bos.write(bytes, 0, len);
        }
        // 刷新缓冲区,写入文件
        bos.flush();
        // 回写数据
        OutputStream os = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();
        socket.close();
        ss.close();
    }
}
